#include "WorkerUnit.h"
#include "ImgMgr.h"
#include "RessourceStruct.h"
#include "Struct.h"
#include "Level.h"


WorkerUnit::WorkerUnit() : Unit()
{
}

void WorkerUnit::setRessourceStruct(RessourceStruct *structure)
{
	m_ressourceStruct = structure;
}

bool WorkerUnit::collisionToRessource()
{
	if(getPosition().x + 8 > m_ressourceStruct->getPosition().x && getPosition().x < m_ressourceStruct->getPosition().x + m_ressourceStruct->getBox().x && getPosition().y + 8 > m_ressourceStruct->getPosition().y && getPosition().y < m_ressourceStruct->getPosition().y + m_ressourceStruct->getBox().y)
		return true;
	else
		return false;
}

void WorkerUnit::init(const int idTeam, const sf::Vector2f position, ImgMgr *imageManager)
{
	int e = 0;
	m_isWorking = false;
	m_haveRessource = false;
	m_ressourceStruct = false;
	while(e < 12)
	{
		m_skin[e] = imageManager->getSprite(89 + e);
		e++;
	}
	m_currentRender = 0;
	m_speed = 0.5;
	m_health = 2;
	m_degat = 0.2f;
	Unit::init(idTeam, position, imageManager);
	setDirection((float)random(0, 360));
}

void WorkerUnit::maj(Level *level)
{
	if(m_isWorking)
	{
		bool collisionU = false;
		bool collisionD = false;
		bool collisionL = false;
		bool collisionR = false;

		if(m_struct->isDestroy())
			m_isWorking = false;
		if(m_ressourceStruct != NULL && m_ressourceStruct->isDestroy())
			m_ressourceStruct = NULL;

		if(m_haveRessource)
		{
			float width = getPosition().x - m_struct->getPosition().x;
			float height = getPosition().y - m_struct->getPosition().y;
			float angle = std::atan2(height, width) / PI * 180;
			setDirection(angle);
		}
		else
		{
			if(m_ressourceStruct == NULL)
			{
				unsigned int e = 0;
				std::vector<RessourceStruct*> *ressourceStruct = level->getRessourceStruct();
				std::vector<RessourceStruct*> m_tempo;
				for(e = 0; e < ressourceStruct->size(); e++)
				{
					if(ressourceStruct->at(e)->getName() == m_struct->getRessourceStruct())
					{
						float posRessourceX = ressourceStruct->at(e)->getPosition().x;
						float posRessourceY = ressourceStruct->at(e)->getPosition().y;
						float posStructX = m_struct->getPosition().x;
						float posStructY = m_struct->getPosition().y;
						int distance = std::sqrt((posRessourceX - posStructX)*(posRessourceX - posStructX)+(posRessourceY - posStructY)*(posRessourceY - posStructY));
						if(distance < 200)
							m_tempo.push_back(ressourceStruct->at(e));
					}
				}
				if(m_tempo.size() > 0)
					m_ressourceStruct = m_tempo[random(0, m_tempo.size())];
			}
			else
			{
				float width = getPosition().x + 8 - (m_ressourceStruct->getPosition().x + m_ressourceStruct->getBox().x / 2);
				float height = getPosition().y + 8 - (m_ressourceStruct->getPosition().y + m_ressourceStruct->getBox().y / 2);
				float angle = std::atan2(height, width) / PI * 180;
				setDirection(angle + 180);
			}
		}
		Unit::maj(level);
	}
	else
	{
		std::vector<Struct*> *structure = level->getStruct();
		unsigned int e = 0;
		for(e = 0; e < structure->size(); e++)
		{
			if(!structure->at(e)->isDestroy())
			{
				if(structure->at(e)->addWorker(this))
				{
					m_struct = structure->at(e);
					m_isWorking = true;
					return;
				}
			}
		}
	}
}